import java.util.LinkedList;


public class Stream extends EnergyAttack{

	private Point source;
	private float direction;
	private float damage;
	private float speed;
	private boolean continuing;
	private boolean life;
	private Sprite sprite;
	private int characterNumber;
	private final int MAX_LENGTH = 400;
	private LinkedList<Blast>blast;
	private int frequency;
	private int count = 0;
	/**
	 * creates a new attack wave
	 * @param source the starting point of the wave
	 * @param direction the direction the wave will advance in
	 * @param damage the amount of damage the wave does.
	 */
	public Stream(Sprite sprite,int frequency,Point source, float direction,float damage, int characterNumber)
	{
		life = true;
		this.radius = 10;
		this.speed = 40;
		this.source = source;
		this.direction = direction;
		this.damage = damage;
		this.continuing = true;
		this.sprite = sprite;
		this.characterNumber = characterNumber;
		blast = new LinkedList<Blast>();
		this.frequency = frequency;
		Point p = new Point(source.getX(),source.getY());
		blast.add(new Blast(sprite,p,direction,damage,characterNumber));
	}
	/**
	 * overwrites the collision detection for normal objects
	 */
	public boolean checkCollision(Collidable c)
	{
		int i = 0;
		while(i<blast.size())
		{
			if(blast.get(i).checkCollision(c) == true)
			{
				return true;
			}
			i++;
		}
		return false;
	}
	
	public boolean isAlive()
	{
		return life;
	}

	public int getCurrentImage()
	{
		return sprite.getImg();
	}
	public float getAngle()
	{
		return direction;
	}
	public int getType()
	{
		return sprite.getType();
	}
	/**
	 * this doesn't collide, the blast's do
	 */
	public void onCollision(Collidable c)
	{
		int i = 0;
		while(i<blast.size())
		{
			if(blast.get(i).checkCollision(c) == true)
			{
				blast.get(i).onCollision(c);
			}
			i++;
		}
	}
	/**
	 * sets the speed of the wave
	 * @param speed
	 */
	public void setSpeed(float speed)
	{
		this.speed = speed;
	}
	public void setRadius(float radius)
	{
		this.radius = radius;
	}
	public void add()
	{
		if(continuing == true)
		{
			if(count >= (31-frequency))
			{
				Point p = new Point(source.getX(),source.getY());
				blast.add(new Blast(sprite,p,direction,damage,characterNumber));
				count = 0;
			}		
		}
		else
		{
			if(count >= (31-frequency))
			{
				blast.remove();
				count = 0;
			}
		}
		if(blast.size() <= 0)
		{
			life = false;
		}
		count++;
		int i = 0;
		while(i<blast.size())
		{
			if(blast.get(i).isAlive() == false)
			{
				blast.remove(i);
			}
			else
			{
				blast.get(i).add();
			}
			i++;
		}
		
	}
	/**
	 * gets a copy of the source location of the wave
	 * @return copy of source
	 */
	public Point getSource()
	{
		return new Point(source.getX(),source.getY());
	}
	/**
	 * tells the wave to stop lengthening itself and just to move forward
	 */
	public void done()
	{
		continuing = false;
	}
	public String getDrawableString()
	{
		String ans = "";
		int i = 0;
		while(i<blast.size())
		{
			ans = ans + blast.get(i).getDrawableString();
			i++;
		}
		System.out.println(ans);
		return ans;
	}
}
